﻿using System.Collections.Generic;

namespace SvGame.Logic;

public static partial class GameLogic
{
    //检查姓名
    public static EProtocolResult CheckPlayerName(string name)
    {
        long puid = GetNamePuid(name);
        if (puid != 0)
        {
            return EProtocolResult.名字已经被使用;
        }
        var rst = NameMgr.CheckName(name);
        if (rst != EProtocolResult.成功)
        {
            return rst;
        }

        return EProtocolResult.成功;
    }
    //存储 
    public static void SaveNamePuid(long Puid, string Name)
    {
        AHashSet(RdsPlayerNamePuid, Puid, Name);
    }
    //读取
    public static long GetNamePuid(string Name)
    {
        return AHashGet(RdsPlayerNamePuid, Name).ToLong();
    }
    public static void CreateSave(Player player)
    {
        //用户在不同服务器的Puid User:Puid:<Uuid> -- ConfigId -- Puid
        DbRedis.SaveUuidPuid(player.Uuid, SvInfo.ConfigId, player.Puid);
        SaveNamePuid(player.Puid, player.Name);
    }
    public static void BindSession(this Player player, MySession Session)
    {
        player.Session = Session;
        Session.Obj = player;
        Session.Puid = player.Puid;
    }
    public static void PlayerCreate(Player player, long puid, C2GPlayerCreate req)
    {
        player.Name = req.Name;
        player.Gender = (EGender)req.Gender;
        player.Head = req.Head;
        player.Family = req.Family;
        player.Talent = req.Talent.Talent();

        //技能
        {
            var 玩家默认技能列表 = EGlobalId.玩家默认技能列表.ValueListString();
            var 玩家默认技能等级 = EGlobalId.玩家默认技能等级.ValueInt();
            foreach (var item in 玩家默认技能列表)
            {
                player.Skills[item] = 玩家默认技能等级;
            }
        }
        // 调试
        // todo PlayerCreate 初始玩家全技能
        if (true)
        {
            foreach (var item in ConfSkill.Values)
            {
                if (player.Skills.Count >= 100)
                {
                    break;
                }
                var Level = Util.Random.Next(1, 511);
                player.Skills[item.Id] = Level;
            }
        }
    }
    /// <summary>
    /// 玩家登入
    /// </summary>
    /// <param name="player"></param>
    public static void PlayerLogin(Player player)
    {
        var dtNow = DateTime.Now;
        player.TimeLogin = dtNow;
        player.TicksUpdateAge = dtNow.Ticks;
        CacheGame.DictPlayer[player.Puid] = player;
        player.Online = true;
        player.TicksAutoSaveSet();

        // 检查玩家身上数据的配置
        PlayerLoginCheck(player);

        player.ActionSet();
        player.PropsEquipSet();
        player.HpSet();

        player.PlayerLoginEnterRoom();
    }
    /// <summary>
    /// 玩家登出
    /// </summary>
    /// <param name="player"></param>
    public static void PlayerLogout(this Player player, bool remove = true)
    {
        loger.Debug($"PlayerLogout {player.Puid} {player.Name}");

        var dtNow = DateTime.Now;
        player.TimeLogout = dtNow;
        
        // 计算年龄
        player.AgeUpdate();

        if (remove)
        {
            DictPlayer.Remove(player.Puid);
        }
    }
    /// <summary>
    /// 检查玩家身上数据的配置
    /// </summary>
    /// <param name="player"></param>
    public static void PlayerLoginCheck(Player player)
    {
        // 道具
        {
            List<long> listNotExist = new();
            foreach (var item in player.DictItem.Values)
            {
                if (!ConfItemExt.TryGetValue(item.ConfigId, out var conf))
                {
                    listNotExist.Add(item.Uid);
                    loger.Error($"玩家 道具 配置ID不存在 {item.ConfigId} UID:{item.Uid}");
                    continue;
                }
                item.Cfg = conf;
            }
            player.DictItem.RemoveAll(listNotExist);
        }
        // 装备
        {
            List<long> listNotExist = new();
            foreach (var item in player.DictEquip.Values)
            {
                if (!ConfEquip.TryGetValue(item.ConfigId, out var conf))
                {
                    listNotExist.Add(item.Uid);
                    loger.Error($"玩家 装备 配置ID不存在 {item.ConfigId} UID:{item.Uid}");
                    continue;
                }
                item.Cfg = conf;
            }
            player.DictEquip.RemoveAll(listNotExist);
        }
    }
}
